require(foreign)
require(xtable)
require(ggplot2)
require(plyr)
require(rdrobust)
require(rdd)
require(stargazer)
require(reshape2)
require(data.table)
require(lfe)
require(RColorBrewer)
rm(list=ls())

ihs <- function(x) log(x + sqrt(x^2+1))

### Load Data from your working directory

load(file="Candidates.Rda")
load(file="ConnectedFirms.Rda")

cons_all_pr<-cons

### Taking out party winners
cons<-subset(cons, party_win==0)

###################################################################
############## ############## Figure 1 ############## ############## 
###################################################################

### See Table A4 below for regional close election data

###################################################################
############## ############## Figure 2 ############## ############## 
###################################################################

# Panel A

binsize = .01
bd = 4
se <- DCdensity(cand$margin[abs(cand$margin)<bd],0, bin= binsize, ext.out=TRUE)$se
t <- DCdensity(cand$margin[abs(cand$margin)<bd],0, bin= binsize, ext.out=TRUE)$theta
t <- round(t, digits = 3)
se <- round(se, digits = 3)
abline(v=0,lty=3, col = 'black')
mtext(paste("Difference=",t), side=1, line=2, cex=.8)
mtext(paste("Standard Error=",se), side=1, line=3, cex=.8)
title(sub="(a) All Candidates", font.main=3)

# Panel B

se <- DCdensity(cand$margin[cand$ur==1 & cand$incumbent==1 &abs(cand$margin)<bd],0, bin= binsize, ext.out=TRUE)$se
t <- DCdensity(cand$margin[cand$ur==1 & cand$incumbent==1 &abs(cand$margin)<bd],0, bin= binsize, ext.out=TRUE)$theta
t <- round(t, digits = 3)
se <- round(se, digits = 3)
abline(v=0,lty=3, col = 'black')
mtext(paste("Diff=",t), side=1, line=2, cex=.8)
mtext(paste("S.E.=",se), side=1, line=3, cex=.8)
title(sub="(b) UR Incumbents", font.main=3)


###################################################################
############## ############## Figure 3 ############## ############## 
###################################################################

dvs <- c("age_start_l", "male", "incumbent","ur","system","nonsystem","director","vote_share.smd")
numv1<-length(dvs)
#### Empty lists to store t-values
## c, : close margin, ll : local linear
## b1, b2 : specific bandwidths chosen ; op : optimal bandwidth
## t : t-value

cm_2_t = rep(0,length(dvs))
cm_3_t = rep(0,length(dvs))
ll_5_t = rep(0,length(dvs))
ll_10_t = rep(0,length(dvs))

cm_2_m_c = list()
ll_5_m_c = list()
cand<-as.data.frame(cand)

for(i in 1:length(dvs)){
  cm_2_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cand, abs(margin)<.02), exactDOF=TRUE)$ctval["okrug_win"]
  
  cm_3_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cand, abs(margin)<.03), exactDOF=TRUE)$ctval["okrug_win"]
  
  ll_5_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win  + margin + margin_i |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cand, abs(margin)<.05), exactDOF=TRUE)$ctval["okrug_win"]
  
  ll_10_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win  + margin + margin_i |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cand, abs(margin)<.12), exactDOF=TRUE)$ctval["okrug_win"]
  
  ## Extracting Coefs for Table A1
  cm_2_m_c[[i]] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cand, abs(margin)<.02), exactDOF=TRUE)
  
  ## Extracting Coefs for Table A1
  ll_5_m_c[[i]] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win + margin + margin_i |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cand, abs(margin)<.05), exactDOF=TRUE)
}

set1_t <- cbind(cm_2_t, cm_3_t, ll_5_t, ll_10_t)

########### FIRM LEVEL ###########

dvs <- c("foreign", "soe","system_firm","agriculture","construction","resources","immobile","totalassets.s.l", "fullturnover.s.l","pmarg.s","leverage.s","taxrate.s","receivedcontracts_regmuni.s")
num_variables = numv1 + length(dvs)

cm_2_t = rep(0,length(dvs))
cm_3_t = rep(0,length(dvs))
ll_5_t = rep(0,length(dvs))
ll_10_t = rep(0,length(dvs))

cm_2_m_f = list()
ll_5_m_f = list()

cons<-as.data.frame(cons)
for(i in 1:length(dvs)){
  cm_2_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cons, abs(margin)<.02), exactDOF=TRUE)$ctval["okrug_win"]
  
  cm_3_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cons, abs(margin)<.03), exactDOF=TRUE)$ctval["okrug_win"]
  
  ll_5_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win  + margin + margin_i |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cons, abs(margin)<.05), exactDOF=TRUE)$ctval["okrug_win"]
  
  ll_10_t[i] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win  + margin + margin_i |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cons, abs(margin)<.12), exactDOF=TRUE)$ctval["okrug_win"]
  
  ## Extracting Coefs for Tables A1-A2
  cm_2_m_f[[i]] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cons, abs(margin)<.02), exactDOF=TRUE)
  
  ## Extracting Coefs for Tables A1-A2
  ll_5_m_f[[i]] = felm(formula(paste(substitute(i,list(i = as.name(dvs[i]))), "~ okrug_win + margin + margin_i |  0 | 0 | cand_id + okrug_id",sep="")), data=subset(cons, abs(margin)<.05), exactDOF=TRUE)
}

set2_t <- cbind(cm_2_t, cm_3_t, ll_5_t, ll_10_t)

covs_labels <- c("Age","Male","Incumbent","United Russia Party","Systemic Opposition","Other Party","Company Director","Previous Vote Share","Foreign-Owned","State-Owned", "Systemic Firm","Agriculture","Construction","Natural Resources","Immobile Assets","Total Assets (logged)", "Revenue (logged)","Profit Margin","Leverage","Tax Rate","Had State Contracts")

tstat<-as.data.frame(rbind(set1_t,set2_t))
tstat$Variable <- covs_labels
mdata <- melt(tstat, id=c("Variable"))

tstat1<-head(mdata, -1*(num_variables))

tstat1<-mdata
levels(tstat1$variable) <- c("Bandwidth = 2%",'Bandwidth = 3%','Bandwidth = 5%',"Bandwidth = 12%")
tstat1$value<-abs(as.numeric(as.character(tstat1$value)))

Estimator = c(rep("Close Margin", num_variables*2),rep("Local Linear", num_variables*2))
tstat1$Estimator <- c(rep("Close Margin", num_variables*2),rep("Local Linear", num_variables*2))
names(tstat1)[2] <- "Sample"
ggplot(tstat1, aes(x = value, y = Variable, shape = Sample, color = Sample)) + geom_point() + geom_vline(xintercept = 2, lty = 2, size = .2) + facet_grid(.~Estimator) + theme_bw() + scale_x_continuous("T Statistic") + scale_color_grey(end = 0)


###################################################################
############## ############## Figure 4 ############## ############## 
###################################################################

### Panel A

breaks <- c(sort(-seq(0, .1, by = .01)[-1]), seq(0, .1, by = .01))
data.span  <- as.data.frame(cons[abs(cons $margin) <= max(breaks), ])
data.span$bins <- cut(data.span$margin, breaks, 
                      include.lowest = TRUE, right = FALSE)

## Choosing control variables
data.span.plot <- as.data.frame(cbind(tapply(data.span$fullturnover.e.l.d, data.span$bins, mean, na.rm = TRUE)))
data.span.N <- as.data.frame(cbind(tapply(data.span$fullturnover.e.l.d, data.span$bins, length)))

colnames(data.span.plot) <- c("fullturnover.e.l.d")
data.span.plot$runvar <- head(breaks, -1)
data.span.plot$runvar = data.span.plot$runvar + .01/2
fig4_panela<-ggplot(data = data.span.plot, mapping=aes(x = runvar, y = fullturnover.e.l.d)) + geom_point() + stat_smooth(data = subset(data.span, margin <=0), aes(x = margin, y= fullturnover.e.l.d),method = loess,span=.8,level=.95,colour="black") + stat_smooth(data = subset(data.span, margin>0), aes(x = margin, y= fullturnover.e.l.d),method = loess,span=.8,level=.95,colour="black")+ theme(text = element_text(size=12), axis.text.x = element_text(vjust=1))+ xlab("Margin of Victory")+ ylab(paste("Change in Logged Total Revenue","",sep=""))+theme_bw()+ggtitle("(a)")+ theme(plot.title = element_text(hjust = 0.5))

### Panel B

breaks <- c(sort(-seq(0, .1, by = .01)[-1]), seq(0, .1, by = .01))
data.span  <- as.data.frame(cons[abs(cons $margin) <= max(breaks), ])
data.span$bins <- cut(data.span$margin, breaks, 
                      include.lowest = TRUE, right = FALSE)

## Choosing control variables
data.span.plot <- as.data.frame(cbind(tapply(data.span$pmarg.d, data.span$bins, mean, na.rm = TRUE)))
data.span.N <- as.data.frame(cbind(tapply(data.span$pmarg.d, data.span$bins, length)))

colnames(data.span.plot) <- c("pmarg.d")
data.span.plot$runvar <- head(breaks, -1)
data.span.plot$runvar = data.span.plot$runvar + .01/2
fig4_panelb<-ggplot(data = data.span.plot, mapping=aes(x = runvar, y = pmarg.d)) + geom_point() + stat_smooth(data = subset(data.span, margin <=0), aes(x = margin, y= pmarg.d),method = loess,span=.8,level=.95,colour="black") + stat_smooth(data = subset(data.span, margin>0), aes(x = margin, y= pmarg.d),method = loess,span=.8,level=.95,colour="black")+ theme(text = element_text(size=12), axis.text.x = element_text(vjust=1))+ xlab("Margin of Victory")+ ylab(paste("Change in Profit Margin","",sep=""))+theme_bw()+ggtitle("(b)")+ theme(plot.title = element_text(hjust = 0.5))



###################################################################
############## ############## TABLE 1 ############## ############## 
###################################################################

## Global - No controls
t1_col1 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
t1_col2 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
t1_col3 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
t1_col4 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
t1_col5 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
t1_col6 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

optb_rev<-rdbwselect(y=cons$fullturnover.e.l,x=cons$margin,c=0,kernel="triangular",p=1,q=3)$bws[1]

## Local Linear - optimal, no controls
t1_col7 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l| 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev), exactDOF=TRUE)

## Local Linear - optimal, controls
t1_col8 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev), exactDOF=TRUE)

## Local Linear - optimal*2, controls
t1_col9 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i +margin_2+margin_i2+ margin_3+margin_i3+fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev*2), exactDOF=TRUE)






###################################################################
############## ############## TABLE 2 ############## ############## 
###################################################################

## Global - No controls
t2_col1 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
t2_col2 <- felm(pmarg.e ~ okrug_win + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l | factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
t2_col3 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
t2_col4 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
t2_col5 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
t2_col6 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

optb_pro<-rdbwselect(y=cons$pmarg.e,x=cons$margin,c=0,kernel="triangular",p=1,q=3)$bws[1]

## Local Linear - optimal, no controls
t2_col7 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro), exactDOF=TRUE)

## Local Linear - optimal, controls
t2_col8 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro), exactDOF=TRUE)

## Local Linear - optimal*2, controls
t2_col9 <- felm(pmarg.e ~ okrug_win+margin+margin_i+margin_2+margin_i2 +margin_3 + margin_i3+ pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro*2), exactDOF=TRUE)





###################################################################
############## ############## TABLE 3 ############## ############## 
###################################################################

t3_col1<-felm(taxrate.e ~ okrug_win + margin+margin_i+ taxrate.s| 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons$taxrate.e,x=cons$margin,c=0,kernel="triangular",p=1,q=3)
optb_tax<-t$bws[1]

t3_col2<-felm(taxrate.e ~ okrug_win + margin+margin_i+ taxrate.s| 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_tax), exactDOF=TRUE)

t3_col3<-felm(taxrate.e ~ okrug_win + margin+margin_i+ taxrate.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_tax), exactDOF=TRUE)

t3_col4<-felm(leverage.e.ihs ~ okrug_win + margin+margin_i+ leverage.s.ihs| 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons$leverage.e.ihs,x=cons$margin,c=0,kernel="triangular",p=1,q=3)
optb_leverage<-t$bws[1]

t3_col5<-felm(leverage.e.ihs ~ okrug_win + margin+margin_i+ leverage.s.ihs| 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_leverage), exactDOF=TRUE)

t3_col6<-felm(leverage.e.ihs ~ okrug_win + margin+margin_i+ leverage.s.ihs + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_leverage), exactDOF=TRUE)

t3_col7<-felm(receivedcontracts_regmuni ~ okrug_win+ margin+margin_i+receivedcontracts_regmuni.s | 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons$receivedcontracts_regmuni,x=cons$margin,c=0,kernel="triangular",p=1,q=3)
optb_contracts<-t$bws[1]

t3_col8<-felm(receivedcontracts_regmuni ~ okrug_win+ margin+margin_i+receivedcontracts_regmuni.s | 0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_contracts), exactDOF=TRUE)

t3_col9<-felm(receivedcontracts_regmuni ~ okrug_win + margin+margin_i + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l+receivedcontracts_regmuni.s| factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_contracts), exactDOF=TRUE)


###################################################################
############## ############## TABLE 4 ############## ############## 
###################################################################

### Setting Medians
dem_score_med<- quantile(cons$reg_democracy_c, probs=c(.5), na.rm=TRUE)
ur_str_med<- quantile(cons$percurseats, probs=c(.5), na.rm=TRUE)

### Democracy Scores
t4_panela_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & reg_democracy_c<dem_score_med), exactDOF=TRUE)

t4_panela_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & reg_democracy_c>=dem_score_med), exactDOF=TRUE)

t4_panela_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & reg_democracy_c< dem_score_med), exactDOF=TRUE)

t4_panela_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & reg_democracy_c>=dem_score_med), exactDOF=TRUE)

### UR Percentage
t4_panelb_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & percurseats< ur_str_med), exactDOF=TRUE)

t4_panelb_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & percurseats>=ur_str_med), exactDOF=TRUE)

t4_panelb_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & percurseats<ur_str_med), exactDOF=TRUE)

t4_panelb_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & percurseats>=ur_str_med), exactDOF=TRUE)

### UR Membership
t4_panelc_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(reg_id) + factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & ur==0), exactDOF=TRUE)

t4_panelc_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(reg_id) + factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & ur==1), exactDOF=TRUE)

t4_panelc_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(reg_id) + factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & ur==0), exactDOF=TRUE)

t4_panelc_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(reg_id) + factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & ur==1), exactDOF=TRUE)


###################################################################
############## ############## TABLE 5 ############## ############## 
###################################################################

grp_low<- quantile(cons$log_grp, probs=c(.5), na.rm=TRUE)


### GRP
t5_panela_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & log_grp<=grp_low), exactDOF=TRUE)

t5_panela_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & log_grp>grp_low), exactDOF=TRUE)

t5_panela_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & log_grp<=grp_low), exactDOF=TRUE)

t5_panela_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & log_grp> grp_low), exactDOF=TRUE)

### Natural Resources
t5_panelb_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & oilgas==0), exactDOF=TRUE)

t5_panelb_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & oilgas==1), exactDOF=TRUE)

t5_panelb_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & oilgas==0), exactDOF=TRUE)

t5_panelb_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & oilgas==1), exactDOF=TRUE)


### Asset Immobility
t5_panelc_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(reg_id)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & immobile==0), exactDOF=TRUE)

t5_panelc_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(reg_id)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & immobile==1), exactDOF=TRUE)

t5_panelc_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(reg_id)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & immobile==0), exactDOF=TRUE)

t5_panelc_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(reg_id)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & immobile==1), exactDOF=TRUE)



###################################################################
############## ############## TABLE 6 ############## ############## 
###################################################################

bus_median<- quantile(cons$percbus, probs=c(.5), na.rm=TRUE)

t6_panela_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector<=1), exactDOF=TRUE)

t6_panela_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector>1), exactDOF=TRUE)

t6_panela_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector<=1), exactDOF=TRUE)

t6_panela_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector>1), exactDOF=TRUE)


t6_panelb_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & percbus<=bus_median), exactDOF=TRUE)

t6_panelb_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & percbus>bus_median), exactDOF=TRUE)

t6_panelb_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & percbus<=bus_median), exactDOF=TRUE)

t6_panelb_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & percbus>bus_median), exactDOF=TRUE)


###################################################################
###################################################################
############## ############## APPENDIX ############## ############## 
###################################################################
###################################################################
###################################################################






###################################################################
############## ############## FIGURE A1 ############## ############## 
###################################################################

binsize = .04
bins=seq(0,1, by=binsize)
hist(cand$margin[cand$okrug_win==1], breaks=bins, ylab="Number of Elections", xlab="Margin of Victory", main=NULL)



###################################################################
############## ############## FIGURE A2 ############## ############## 
###################################################################

breaks <- c(seq(0.01, .1, by = .005))
bd.dta <- data.frame(bd= numeric(0), coef= numeric(0),se= numeric(0))
for(i in 1:length(breaks)){
  bd = breaks[i]
  t_ll <-	felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id, data=subset(cons, abs(margin)<bd), exactDOF=TRUE)
  
  bd = breaks[i]
  bd.dta[i, ] <- c(bd, as.vector(t_ll$coefficients)[2],t_ll$se["okrug_win"])
}
bd.dta$margin<-as.numeric(bd.dta$bd)*100	
bd.dta$coef<-as.numeric(bd.dta$coef)
bd.dta$se<-as.numeric(bd.dta$se)

p1 <- ggplot(bd.dta, aes(margin, coef))+
  geom_line(data=bd.dta)+
  geom_ribbon(data= bd.dta,aes(ymin=coef-1.96*se,ymax=coef+1.96*se),alpha=0.3)+ scale_x_continuous(breaks=seq(0, 10, 1))+ xlab("Threshold Value: Margin of Victory (%)")+ ylab("Estimate")+ labs(title="Local Linear Model")+ scale_y_continuous(limits=c(-2.5,2.5))

bd.dta <- data.frame(bd= numeric(0), coef= numeric(0),se= numeric(0))
for(i in 1:length(breaks)){
  bd = breaks[i]
  t_ll <-	felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l|  0 | 0 | cand_id, data=subset(cons, abs(margin)<bd), exactDOF=TRUE)
  
  bd = breaks[i]
  bd.dta[i, ] <- c(bd, as.vector(t_ll$coefficients)[2],t_ll$se["okrug_win"])
}
bd.dta$margin<-as.numeric(bd.dta$bd)*100	
bd.dta$coef<-as.numeric(bd.dta$coef)
bd.dta$se<-as.numeric(bd.dta$se)

p2 <- ggplot(bd.dta, aes(margin, coef))+
  geom_line(data=bd.dta)+
  geom_ribbon(data= bd.dta,aes(ymin=coef-1.96*se,ymax=coef+1.96*se),alpha=0.3)+ scale_y_continuous(limits=c(-2.5,2.5))+ scale_x_continuous(breaks=seq(0, 10, 1))+ xlab("Threshold Value: Margin of Victory (%)")+ ylab("Estimate")+ labs(title="Close Margin Model")



###################################################################
############## ############## FIGURE A3 ############## ############## 
###################################################################


breaks <- c(seq(0.01, .1, by = .005))
bd.dta <- data.frame(bd= numeric(0), coef= numeric(0),se= numeric(0))
for(i in 1:length(breaks)){
  bd = breaks[i]
  t_ll <-	felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id, data=subset(cons, abs(margin)<bd), exactDOF=TRUE)
  
  bd = breaks[i]
  bd.dta[i, ] <- c(bd, as.vector(t_ll$coefficients)[2],t_ll$se["okrug_win"])
}
bd.dta$margin<-as.numeric(bd.dta$bd)*100	
bd.dta$coef<-as.numeric(bd.dta$coef)
bd.dta$se<-as.numeric(bd.dta$se)

p1 <- ggplot(bd.dta, aes(margin, coef))+
  geom_line(data=bd.dta)+
  geom_ribbon(data= bd.dta,aes(ymin=coef-1.96*se,ymax=coef+1.96*se),alpha=0.3)+ scale_x_continuous(breaks=seq(0, 10, 1))+ xlab("Threshold Value: Margin of Victory (%)")+ ylab("Estimate")+ labs(title="Local Linear Model")+ scale_y_continuous(limits=c(-0.75,0.75))

bd.dta <- data.frame(bd= numeric(0), coef= numeric(0),se= numeric(0))
for(i in 1:length(breaks)){
  bd = breaks[i]
  t_ll <-	felm(pmarg.e ~ okrug_win + pmarg.s|  0 | 0 | cand_id, data=subset(cons, abs(margin)<bd), exactDOF=TRUE)
  
  bd = breaks[i]
  bd.dta[i, ] <- c(bd, as.vector(t_ll$coefficients)[2],t_ll$se["okrug_win"])
}
bd.dta$margin<-as.numeric(bd.dta$bd)*100	
bd.dta$coef<-as.numeric(bd.dta$coef)
bd.dta$se<-as.numeric(bd.dta$se)

p2 <- ggplot(bd.dta, aes(margin, coef))+
  geom_line(data=bd.dta)+
  geom_ribbon(data= bd.dta,aes(ymin=coef-1.96*se,ymax=coef+1.96*se),alpha=0.3)+ scale_y_continuous(limits=c(-0.75,0.75))+ scale_x_continuous(breaks=seq(0, 10, 1))+ xlab("Threshold Value: Margin of Victory (%)")+ ylab("Estimate")+ labs(title="Close Margin Model")





###################################################################
############## ############## TABLES A1-A3 ############## ############## 
###################################################################

### See Figure 3 above for regressions that are used to generate these tables




###################################################################
############## ############## TABLE A4 ############## ############## 
###################################################################

el<-as.data.table(cand)[, list(no_parties=uniqueN(party_sub), male=mean(male), age=mean(age_start_l), voterlist=mean(as.numeric(num_voters)), incumbent = max(incumbent, na.rm = TRUE), margin = max(margin, na.rm = TRUE), year = max(actualstartyear), midterm = max(midterm),ur_win = max(ur_win, na.rm = TRUE),reg_id=max(reg_id)), by=c("okrug_id")]

el$year <- factor(el$year)
el$log_vote <- log(el$voterlist)

el$close_05[el$margin<.05]<-1
el$close_05[el$margin>=.05]<-0

el$close_10[el$margin<.10]<-1
el$close_10[el$margin>=.10]<-0

el$close_20[el$margin<.20]<-1
el$close_20[el$margin>=.20]<-0

el$close_35[el$margin<.35]<-1
el$close_35[el$margin>=.35]<-0

clx <-function(fm, dfcw, cluster){
  library(sandwich)
  library(lmtest)
  M <- length(unique(cluster))
  N <- length(cluster)
  dfc <- (M/(M-1))*((N-1)/(N-fm$rank))
  u <- apply(estfun(fm),2,
             function(x) tapply(x, cluster, sum))
  vcovCL <- dfc*sandwich(fm, meat=crossprod(u)/N)*dfcw
  coeftest(fm, vcovCL) }

ta4_col1 <- clx(glm(close_05 ~ no_parties +ur_win+ male + age+ incumbent + midterm + log_vote, data=el, family="binomial"), 1, el$reg_id)

ta4_col2 <- clx(glm(close_10 ~ no_parties +ur_win+ male + age+incumbent + midterm + log_vote, data=el, family="binomial"), 1, el$reg_id)

ta4_col3 <- clx(glm(close_20 ~ no_parties +ur_win+ male + age+incumbent + midterm + log_vote, data=el, family="binomial"), 1, el$reg_id)

ta4_col4 <- clx(glm(close_35 ~ no_parties +ur_win+ male + age+incumbent + midterm + log_vote, data=el, family="binomial"), 1, el$reg_id)

### Combine Regions for Mapping Purposes (double IDs)
el$reg_id[el$reg_id==18]<-10   ### Moscow and Moscow Oblast
el$reg_id[el$reg_id==80]<-87   ### Yakutia and Sakhalin
el$reg_id[el$reg_id==79]<-64   ### Altai and Agin

## Take out Murmansk and Koryak which only have one non-multimember SMD 
el<-subset(el, reg_id!=26 & reg_id!=85)

### Statistics used to create Figure 1
regel<-el[,list(perclose10=mean(close_10)),by=c("reg_id")]


###################################################################
############## ############## TABLE A5 ############## ############## 
###################################################################

keeps = c("age_start_l", "male", "incumbentnew","ur","system","nonsystem","director_nofin","vote_share.smd")
cand_keeps <- as.data.frame(cand)[,(names(cand) %in% keeps)]

maintable<-stargazer(cand_keeps, label="summary",title="Summary Statistics", header=FALSE, column.sep.width="-0.5pt")

keeps=c("foreign", "soe","system_firm","agriculture","construction","resources","immobile","totalassets.s.l", "fullturnover.s.l","pmarg.s","leverage.s","taxrate.s","receivedcontracts_regmuni.s","totalassets.e.l", "fullturnover.e.l","pmarg.e","leverage.e","taxrate.e","receivedcontracts_regmuni","reg_democracy_added_c","percurseats","log_grp","oilgas","numcandspersector_nofinance","percbus")

cons_keeps <- as.data.frame(cons)[,(names(cons) %in% keeps)]

command <- 'stargazer(cons_keeps,order=c("foreign", "soe","system_firm","agriculture","construction","resources","immobile","totalassets.s.l", "fullturnover.s.l","pmarg.s","leverage.s","taxrate.s","receivedcontracts_regmuni.s","totalassets.e.l", "fullturnover.e.l","pmarg.e","leverage.e","taxrate.e","receivedcontracts_regmuni","reg_democracy_added_c","percurseats","log_grp","oilgas","numcandspersector_nofinance","percbus"))'

stargazer.keeprow<-function(call="") {
  command<-gsub("\\)$",", table.layout='t'\\)",command)
  call<-capture.output(eval(parse(text=command)))
  bounds<-grep("tabular",call)
  row1<- call[(bounds[1]+1):(bounds[2]-1)]
  row1<-gsub("^\\\\\\\\\\[-1\\.8ex\\]\\\\hline $", "", row1)
  row1<-gsub("^\\\\hline \\\\\\\\\\[-1\\.8ex\\] $","",row1)
  row1<-row1[row1!=""]
}
extrarows<-stargazer.keeprow(command)
extrarows<-extrarows[2:length(extrarows)]

table<-c(maintable[1:17],extrarows, maintable[18:20])

###################################################################
############## ############## TABLE A6 ############## ############## 
###################################################################

optb_rev<-rdbwselect(y=cons$fullturnover.e.l,x=cons$margin,c=0,kernel="triangular",p=1,q=3)$bws[1]
optb_pro<-rdbwselect(y=cons$pmarg.e,x=cons$margin,c=0,kernel="triangular",p=1,q=3)$bws[1]

##### NO RIVALS
ta6_panela_c1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector<=0), exactDOF=TRUE)

ta6_panela_c2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector>0), exactDOF=TRUE)

ta6_panela_c3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector<=0), exactDOF=TRUE)

ta6_panela_c4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector>0), exactDOF=TRUE)

##### TWO RIVALS

ta6_panelb_c1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector<=2), exactDOF=TRUE)

ta6_panelb_c2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector>2), exactDOF=TRUE)

ta6_panelb_c3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector<=2), exactDOF=TRUE)

ta6_panelb_c4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector>2), exactDOF=TRUE)

##### FOUR RIVALS

ta6_panelc_c1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector<=4), exactDOF=TRUE)

ta6_panelc_c2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & numcandspersector>4), exactDOF=TRUE)

ta6_panelc_c3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) + factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector<=4), exactDOF=TRUE)

ta6_panelc_c4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(reg_id) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & numcandspersector>4), exactDOF=TRUE)


###################################################################
############## ############## TABLE A7 ############## ############## 
###################################################################

## Global - No controls
ta7_c1 <- felm(fullturnover.average.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta7_c2 <- felm(fullturnover.average.l ~ okrug_win + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta7_c3 <- felm(fullturnover.average.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta7_c4 <- felm(fullturnover.average.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta7_c5 <- felm(fullturnover.average.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta7_c6 <- felm(fullturnover.average.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

optb_rev<-rdbwselect(y=cons$fullturnover.average.l,x=cons$margin,c=0,kernel="triangular",p=1,q=3)$bws[1]

## Local Linear - optimal, no controls
ta7_c7 <- felm(fullturnover.average.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev), exactDOF=TRUE)

## Local Linear - optimal, controls
ta7_c8 <- felm(fullturnover.average.l ~ okrug_win+margin+margin_i + fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev), exactDOF=TRUE)

## optimal*2, controls
ta7_c9 <- felm(fullturnover.average.l ~ okrug_win+margin+margin_i +margin_2+margin_i2+ margin_3+margin_i3+fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev*2), exactDOF=TRUE)


###################################################################
############## ############## TABLE A8 ############## ############## 
###################################################################

## Global - No controls
ta8_c1 <- felm(pmarg.average ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta8_c2 <- felm(pmarg.average ~ okrug_win + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l | factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta8_c3 <- felm(pmarg.average ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta8_c4 <- felm(pmarg.average ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta8_c5 <- felm(pmarg.average ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta8_c6 <- felm(pmarg.average ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<.05), exactDOF=TRUE)

optb_pro<-rdbwselect(y=cons$pmarg.average,x=cons$margin,c=0,kernel="triangular",p=1,q=3)$bws[1]

## Local Linear - optimal, no controls
ta8_c7 <- felm(pmarg.average ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro), exactDOF=TRUE)

## Local Linear - optimal, controls
ta8_c8 <- felm(pmarg.average ~ okrug_win+margin+margin_i + pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro), exactDOF=TRUE)

## 2*Optimal Bandwidth, controls
ta8_c9 <- felm(pmarg.average ~ okrug_win+margin+margin_i+margin_2+margin_i2 +margin_3 + margin_i3+ pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro*2), exactDOF=TRUE)


###################################################################
############## ############## TABLE A9 ############## ############## 
###################################################################


## Global - No controls
ta7_c1 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta7_c2 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta7_c3 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta7_c4 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta7_c5 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta7_c6 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons,director_full==1 &  abs(margin)<.05), exactDOF=TRUE)

optb_rev<-rdbwselect(y=cons$fullturnover.e.l[cons$director_full==1],x=cons$margin[cons$director_full==1],c=0,kernel="triangular",p=1,q=3)$bws[1]

## Local Linear - optimal, no controls
ta7_c7 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<optb_rev), exactDOF=TRUE)

## Local Linear - optimal, controls
ta7_c8 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<optb_rev), exactDOF=TRUE)

## 2*optimal, controls
ta7_c9 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i +margin_2+margin_i2+ margin_3+margin_i3+fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<optb_rev*2), exactDOF=TRUE)


###################################################################
############## ############## TABLE A10 ############## ############## 
###################################################################

## Global - No controls
ta8_c1 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons,director_full==1 &  abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta8_c2 <- felm(pmarg.e ~ okrug_win + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l | factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta8_c3 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta8_c4 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta8_c5 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta8_c6 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<.05), exactDOF=TRUE)

optb_pro<-rdbwselect(y=cons$pmarg.e[cons$director_full==1],x=cons$margin[cons$director_full==1],c=0,kernel="triangular",p=1,q=3)$bws[1]

## Local Linear - optimal, no controls
ta8_c7 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<optb_pro), exactDOF=TRUE)

## Local Linear - optimal, controls
ta8_c8 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<optb_pro), exactDOF=TRUE)

## 2*Optimal Bandwidth, controls
ta8_c9 <- felm(pmarg.e ~ okrug_win+margin+margin_i+margin_2+margin_i2 +margin_3 + margin_i3+ pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, director_full==1 & abs(margin)<optb_pro*2), exactDOF=TRUE)

###################################################################
############## ############## TABLE A11 ############## ############## 
###################################################################

## Global - No controls
ta11_c1 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta11_c2 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta11_c3 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta11_c4 <- felm(fullturnover.e.l ~ okrug_win + fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta11_c5 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta11_c6 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons,pr_and_smd==0 &  abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons$fullturnover.e.l[cons$pr_and_smd==0],x=cons$margin[cons$pr_and_smd==0],c=0,kernel="triangular",p=1,q=3)
optb_rev<-t$bws[1]

## Local Linear - optimal, no controls
ta11_c7 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<optb_rev), exactDOF=TRUE)

## Local Linear - optimal, controls
ta11_c8 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<optb_rev), exactDOF=TRUE)

## 2*optimal, controls
ta11_c9 <- felm(fullturnover.e.l ~ okrug_win+margin+margin_i +margin_2+margin_i2+ margin_3+margin_i3+fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<optb_rev*2), exactDOF=TRUE)




###################################################################
############## ############## TABLE A12 ############## ############## 
###################################################################

## Global - No controls
ta12_c1 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons,pr_and_smd==0 &  abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta12_c2 <- felm(pmarg.e ~ okrug_win + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l | factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta12_c3 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta12_c4 <- felm(pmarg.e ~ okrug_win + pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta12_c5 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta12_c6 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons$pmarg.e[cons$pr_and_smd==0],x=cons$margin[cons$pr_and_smd==0],c=0,kernel="triangular",p=1,q=3)
optb_pro<-t$bws[1]

## Local Linear - optimal, no controls
ta12_c7 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<optb_pro), exactDOF=TRUE)

## Local Linear - optimal, controls
ta12_c8 <- felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<optb_pro), exactDOF=TRUE)

## Optimal Bandwidth, controls
ta12_c9 <- felm(pmarg.e ~ okrug_win+margin+margin_i+margin_2+margin_i2 +margin_3 + margin_i3+ pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, pr_and_smd==0 & abs(margin)<optb_pro*2), exactDOF=TRUE)


###################################################################
############## ############## TABLE A13 ############## ############## 
###################################################################

## Global - No controls
ta13_c1 <- felm(fullturnover.e.l ~ okrug_win + party_win +  fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta13_c2 <- felm(fullturnover.e.l ~ okrug_win + party_win +  fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l| factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta13_c3 <- felm(fullturnover.e.l ~ okrug_win + party_win +  fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta13_c4 <- felm(fullturnover.e.l ~ okrug_win + party_win +  fullturnover.s.l |  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta13_c5 <- felm(fullturnover.e.l ~ okrug_win+party_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta13_c6 <- felm(fullturnover.e.l ~ okrug_win+party_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons_all_pr$fullturnover.e.l,x=cons_all_pr$margin,c=0,kernel="triangular",p=1,q=2)
optb_rev<-t$bws[1]

## Local Linear - optimal, no controls
ta13_c7 <- felm(fullturnover.e.l ~ okrug_win+party_win+margin+margin_i + fullturnover.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<optb_rev), exactDOF=TRUE)

## Local Linear - optimal, controls
ta13_c8 <- felm(fullturnover.e.l ~ okrug_win+party_win+margin+margin_i + fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<optb_rev), exactDOF=TRUE)

## 2*optimal, controls
ta13_c9 <- felm(fullturnover.e.l ~ okrug_win+party_win+margin+margin_i +margin_2+margin_i2+ margin_3+margin_i3+fullturnover.s.l + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<optb_rev*2), exactDOF=TRUE)





###################################################################
############## ############## TABLE A14 ############## ############## 
###################################################################

## Global - No controls
ta14_c1 <- felm(pmarg.e ~ okrug_win + party_win +  pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.8), exactDOF=TRUE)

## Global - Controls
ta14_c2 <- felm(pmarg.e ~ okrug_win + party_win +  pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l | factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.8), exactDOF=TRUE)

## Close Margin - 2
ta14_c3 <- felm(pmarg.e ~ okrug_win + party_win +  pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.02), exactDOF=TRUE)

## Close Margin - 3
ta14_c4 <- felm(pmarg.e ~ okrug_win + party_win +  pmarg.s |  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.03), exactDOF=TRUE)

## Local Linear - 5, no controls
ta14_c5 <- felm(pmarg.e ~ okrug_win+party_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.05), exactDOF=TRUE)

## Local Linear - 5, controls
ta14_c6 <- felm(pmarg.e ~ okrug_win+party_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<.05), exactDOF=TRUE)

t<-rdbwselect(y=cons_all_pr$pmarg.e,x=cons_all_pr$margin,c=0,kernel="triangular",p=1,q=2)
optb_pro<-t$bws[1]

## Local Linear - optimal, no controls
ta14_c7 <- felm(pmarg.e ~ okrug_win+party_win+margin+margin_i + pmarg.s|  0 | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<optb_pro), exactDOF=TRUE)

## Local Linear - optimal, controls
ta14_c8 <- felm(pmarg.e ~ okrug_win+party_win+margin+margin_i + pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<optb_pro), exactDOF=TRUE)

## 2*Optimal Bandwidth, controls
ta14_c9 <- felm(pmarg.e ~ okrug_win+party_win+margin+margin_i+margin_2+margin_i2 +margin_3 + margin_i3+ pmarg.s + ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) +factor(reg_id) + factor(sector) | 0 | cand_id + okrug_id, data=subset(cons_all_pr, abs(margin)<optb_pro*2), exactDOF=TRUE)

###################################################################
############## ############## TABLE A15 ############## ############## 
###################################################################



### Setting Medians
dem_score_med<- quantile(cons$dem_residuals, probs=c(.5), na.rm=TRUE)
ur_str_med<- quantile(cons$ur_residuals, probs=c(.5), na.rm=TRUE)

### Democracy Scores
ta15_panela_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & dem_residuals<dem_score_med), exactDOF=TRUE)

ta15_panela_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & dem_residuals>=dem_score_med), exactDOF=TRUE)

ta15_panela_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & dem_residuals< dem_score_med), exactDOF=TRUE)

ta15_panela_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & dem_residuals>=dem_score_med), exactDOF=TRUE)

### UR Percentage
ta15_panelb_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & ur_residuals<=ur_str_med), exactDOF=TRUE)

ta15_panelb_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear)+ factor(sector) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & ur_residuals> ur_str_med), exactDOF=TRUE)

ta15_panelb_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & ur_residuals<=ur_str_med), exactDOF=TRUE)

ta15_panelb_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(actualendyear) + factor(sector)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & ur_residuals> ur_str_med), exactDOF=TRUE)


###################################################################
############## ############## TABLE A16 ############## ############## 
###################################################################

cons$elections<-cons$civil_soc_c+cons$media_c+cons$openness_c+cons$pluralism_c+cons$elecs_c+cons$pol_struc_c

elec_mean<-median(cons$elections,na.rm=TRUE)

### Democracy Scores
ta16_col1<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & elections<elec_mean), exactDOF=TRUE)

ta16_col2<-felm(fullturnover.e.l ~ okrug_win+margin+margin_i + fullturnover.s.l+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector)+ factor(actualendyear) | 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_rev & elections>=elec_mean), exactDOF=TRUE)

ta16_col3<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & elections<elec_mean), exactDOF=TRUE)

ta16_col4<-felm(pmarg.e ~ okrug_win+margin+margin_i + pmarg.s+ ur +male + incumbent + age_start_l+soe+foreign+totalassets.s.l|  factor(sector) + factor(actualendyear)| 0 | cand_id + okrug_id, data=subset(cons, abs(margin)<optb_pro & elections>=elec_mean), exactDOF=TRUE)

